package com.djzhou.gmms.authority.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.djzhou.gmms.base.action.BaseAction;
import com.djzhou.gmms.model.authority.TbAuthority;
import com.opensymphony.xwork2.ModelDriven;

public class AuthorityAction extends BaseAction<TbAuthority> implements ModelDriven<TbAuthority> {

	private TbAuthority authority;
	private Integer authorityId;
	private List<TbAuthority> list;
	
	private List<TbAuthority> defaultAuthorityList;
	

	@Override
	public String delete() {
		authorityService.delete(authorityId);
		msg="删除成功";
		setCurrentPage(currentPage);
		logService.addLog("权限管理",queryUser(),"删除",authorityId,"TbAuthority",super.queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String modify() {
		authority.setLastModifyTime(new Date());
		authority.setUser(queryUser());
		authority.setCompany(queryCompany());
		authorityService.update(authority);
		msg="修改成功";
		setCurrentPage(currentPage);
		logService.addLog("权限管理",queryUser(),"编辑",authorityId,"TbAuthority",super.queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String queryList() {
		if(loadData==null)
			return SUCCESS;
		HashMap<String, Object> hashMap=new HashMap<String, Object>();
		hashMap.put("companyId", queryCompany().getCompanyId());
		
		pageBean=authorityService.queryPageBean(hashMap, pageSize, currentPage);
		list=pageBean.getList();
		if(currentPage>1){
			if(list==null || list.size()==0){
				setCurrentPage(getCurrentPage()-1);
				pageBean=authorityService.queryPageBean(hashMap, pageSize, currentPage);
				list=pageBean.getList();
			}
		}
		if(list!=null){
			totalCount=pageBean.getAllRow();
		}else{
			totalCount = 0;
			list = new ArrayList<TbAuthority>();
		}
		totalPage=pageBean.getTotalPage();
		return SUCCESS;
	}

	@Override
	public String save() {
		if(authority!=null){
			Date date=new Date();
			authority.setCreateTime(date);
			authority.setLastModifyTime(date);
			authority.setDeleteFlag(0);
			authority.setCompany(queryCompany());
			authority.setUser(queryUser());
			authorityService.add(authority);
		}
		setCurrentPage(currentPage);
		logService.addLog("权限管理",queryUser(),"添加",authorityId,"TbAuthority",super.queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String toAdd() {
		defaultAuthorityList=authorityService.queryDefaultAuthorityList();
		return SUCCESS;
	}

	@Override
	public String toEdit() {
		setCurrentPage(currentPage);
		defaultAuthorityList=authorityService.queryDefaultAuthorityList();
		authority=authorityService.queryById(authorityId);
		return SUCCESS;
	}

	public TbAuthority getAuthority() {
		return authority;
	}

	public void setAuthority(TbAuthority authority) {
		this.authority = authority;
	}

	public Integer getAuthorityId() {
		return authorityId;
	}

	public void setAuthorityId(Integer authorityId) {
		this.authorityId = authorityId;
	}

	public List<TbAuthority> getList() {
		return list;
	}

	public void setList(List<TbAuthority> list) {
		this.list = list;
	}
	
	public TbAuthority getModel() {
		return authority;
	}

	public List<TbAuthority> getDefaultAuthorityList() {
		return defaultAuthorityList;
	}

	public void setDefaultAuthorityList(List<TbAuthority> defaultAuthorityList) {
		this.defaultAuthorityList = defaultAuthorityList;
	}


}
